﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Collections.Specialized;
namespace Enterprise.Utility
{
    /// <summary>
    /// 关于页面的一些简单扩展
    /// </summary>
    public static  class PageExtend {

        /// <summary>
        /// 格式化单引号的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string FormatStrSingle(string str) {
            return str.Replace("\\", "\\\\").Replace("'", "\'"); ;
        }

        /// <summary>
        /// 弹出消息 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        public static void Alert(this Page page, string msg) {
            msg = FormatStrSingle(msg);
            string js="alert('"+ msg +"');";
            page.ClientScript.RegisterStartupScript(page.GetType(), "alter", js, true);
        }
        /// <summary>
        /// 弹出消息,并用js来reload页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        public static void AlertAnReload(this Page page, string msg) {
            msg = FormatStrSingle(msg);
            string js = "alert('" + msg + "');window.location.reload();";
            page.ClientScript.RegisterStartupScript(page.GetType(), "alter", js, true);
        }
        /// <summary>
        /// 弹出消息,并用href+="",来重新打开页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        public static void AlertAnReOpen(this Page page, string msg) {
            msg = FormatStrSingle(msg);
            string js = "alert('" + msg + "');window.location.href+='';";
            page.ClientScript.RegisterStartupScript(page.GetType(), "alter", js, true);
        }
        /// <summary>
        /// 弹出消息 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        public static void AlertAnBack(this Page page, string msg) {
            msg = FormatStrSingle(msg);
            string js = "alert('" + msg + "');history.go(-1);";
            page.ClientScript.RegisterStartupScript(page.GetType(), "alter", js, true);
        }

        /// <summary>
        /// 执行JS代码 
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="js">JS代码 不需要添加script标签</param>
        public static void RunJs(this Page page, string js) {
            page.ClientScript.RegisterStartupScript(page.GetType(), "runjs", js, true);
        }
        /// <summary>
        /// 执行JS代码 
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="js">JS代码 不需要添加script标签</param>
        /// <param name="jskey">JS代码的key（当同一页面需要注册多个脚本时用到）</param>
        public static void RunJs(this Page page, string js, string jskey)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), jskey, js, true);
        }

        /// <summary>
        /// 弹出消息 后,页面跳转
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        /// <param name="url"></param>
        public static void AlertAndRedirect(this Page page, string msg, string url) {
            msg = FormatStrSingle(msg);
            string js = "alert('" + msg + "'); window.location.href='"+ url +"';";
            page.ClientScript.RegisterStartupScript(page.GetType(), "alterandredirect", js, true);
        }

        /// <summary>
        /// 弹出消息 后,页面跳转,会调用Response.End()
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        /// <param name="url"></param>
        public static void AlertAndRedirectBeforePageLoad(this Page page, string msg, string url) {
            msg = FormatStrSingle(msg);
            string js = "alert('" + msg + "'); window.location.href='" + url + "';";
            page.Response.Write("<script type=\"text/javascript\">" + js + "</script>");
            page.Response.End();
        }
        /// <summary>
        /// 弹出消息 后,页面返回,会调用Response.End()
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        /// <param name="url"></param>
        public static void AlertAndBackBeforePageLoad(this Page page, string msg) {
            msg = FormatStrSingle(msg);
            string js = "alert('" + msg + "');history.go(-1);";
            page.Response.Write("<script type=\"text/javascript\">" + js + "</script>");
            page.Response.End();
        }

        /// <summary>
        /// 打开一个地址
        /// </summary>
        /// <param name="page"></param>
        /// <param name="url"></param>
        public static void Open(this Page page, string url) {
            url = FormatStrSingle(url);
            string js = "window.location.href='" + url + "';";
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "open", js, true);
        }
        /// <summary>
        /// 打开一个地址
        /// </summary>
        /// <param name="page"></param>
        /// <param name="url"></param>
        public static void Open(this Page page, string url, string target) {
            url = FormatStrSingle(url);
            target = FormatStrSingle(target);
            string js = "window.open('" + url + "','"+ target  +"');";
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "openwin", js, true);
        }

        /// <summary>
        /// 弹出确认框 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message"></param>
        /// <param name="iftrue"></param>
        /// <param name="iffalse"></param>
        public static void Confirm(this Page page, string message, string iftrue, string iffalse) {
            message = FormatStrSingle(message);
            StringBuilder sb = new StringBuilder("if(confirm('"+ message +"')){");
            sb.Append(iftrue);
            sb.Append("}else{");
            sb.Append(iffalse);
            sb.Append("};");
            page.ClientScript.RegisterStartupScript(page.GetType(), "confirm", sb.ToString(), true);
        }

        /// <summary>
        /// 替换部分参数，返回替换参数后的地址
        /// </summary>
        /// <param name="page"></param>
        /// <param name="toUpdateQueryString"></param>
        public static string  GetUrlReplaceQuerystring(this Page page,  NameValueCollection toUpdateQueryString) {
            NameValueCollection quest = page.Request.QueryString;
            string url = page.Request.Url.ToString();
            if (url.IndexOf("?") > 0) {
                url = url.Substring(0, url.IndexOf("?"));
            }

            //将旧的参数存放在这
            Dictionary<string, string> newArguments = new Dictionary<string, string>();
            foreach (string key in quest.Keys) {
                newArguments[key] = quest[key];
            }
            //将新的参数更新过来
            foreach (string key in toUpdateQueryString.Keys) {
                newArguments[key] = toUpdateQueryString[key];
            } 

            string span = "?";


            foreach (string key in newArguments.Keys) {
                url += (span + key + "=" + page.Server.UrlEncode(newArguments[key]));
                span = "&";
            }
            return url;
        }
    }
}
